package cn.udrm.water.base.v1.manager.entities;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedAttributeNode;
import javax.persistence.NamedEntityGraph;
import javax.persistence.NamedEntityGraphs;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import org.hibernate.annotations.GenericGenerator;

/**
 * 管理单位(部门)
 * @author liukang
 * @date 2018年11月18日
 */
@Getter
@Setter
@NoArgsConstructor
@Entity(name="BaseDepartment")
@Table(name="BASE_ORG_DEPARTMENT")
@NamedEntityGraphs({
	@NamedEntityGraph(name="BaseDepartment.organization",
			attributeNodes={
			@NamedAttributeNode("organization")
	}),
	@NamedEntityGraph(name="BaseDepartment.parent",
		attributeNodes={
		@NamedAttributeNode("parent")
	}),
	@NamedEntityGraph(name="BaseDepartment.childrens",
		attributeNodes={
		@NamedAttributeNode("childrens")
	}),
	@NamedEntityGraph(name="BaseDepartment.persons",
		attributeNodes={
		@NamedAttributeNode("persons")
	}),
	@NamedEntityGraph(name="BaseDepartment.roles",
		attributeNodes={
		@NamedAttributeNode("roles")
	}),
	@NamedEntityGraph(name="BaseDepartment.rolesAndParentAndOrg",
		attributeNodes={
		@NamedAttributeNode("roles"),
		@NamedAttributeNode("parent"),
		@NamedAttributeNode("organization")
	})
})
public class BaseDepartment implements Serializable {
	private static final long serialVersionUID = 1L;
	@Id
	@GenericGenerator(name="idGenerator", strategy="uuid")
	@GeneratedValue(generator="idGenerator")
	@Column(name="ID")
	private String id;
	@Column(name="ENTITY_CODE")
	private String entityCode;
	@Column(name="CREATE_DATE")
	private Date createDate;
	@Column(name="PERSON_GUID")
	private String personGuid;
	@Column(name="PERSON_NAME")
	private String personName;
	@Column(name="NAME")
	private String name;
	
	/*
	 * 管理单位级别信息
	 * 处所段
	 * 水管站、水务局
	 */
	@Column(name="MANAGER_LEVEL")
	private String managerLevel;
	
	/*
	 * 部门是否是虚拟部门
	 */
	@Column(name="VIRTUAL_NODE")
	private Boolean virtualNode;
	
	/*
	 * 经度
	 */
	@Column(name="LONGITUDE")
	private String longitude;
	/*
	 * 纬度
	 */
	@Column(name="LATITUDE")
	private String latitude;
	/*
	 * 管理单位所在地
	 */
	@Column(name="OFFICE_LOCATION")
	private String officeLocation;
	/*
	 * 办公电话
	 */
	@Column(name="OFFICE_TEL")
	private String officeTel;
	/*
	 * 办公室联系人员
	 */
	@Column(name="OFFICE_PERSON_NAME")
	private String officePersonName;
	
	@Column(name="INFO_REMARKS", length=2048)
	private String infoRemarks;
	/*
	 * 部门自关联
	 */
	@ManyToOne(optional=true)
	@JoinColumn(name="PARENT_ID")
	private BaseDepartment parent;
	@OneToMany(mappedBy="parent")
	private List<BaseDepartment> childrens;
	
	@JoinColumn(name="ORGANIZATION_GUID")
	@ManyToOne(optional=true, fetch=FetchType.LAZY)
	private BaseOrganization organization;
	
	@OneToMany(mappedBy="department")
	private List<BasePerson> persons;
	
	/*
	 * 部门所属角色
	 */
	@ManyToMany
    @JoinTable(name="BASE_ORG_DEPARENT_ROLE", joinColumns=@JoinColumn(name="DEPARENT_GUID"),
    			inverseJoinColumns=@JoinColumn(name="ROLE_GUID"))
	private List<BaseRole> roles;
	
	public BaseDepartment(String id) {
		this.id = id;
	}
	
}
